package com.ll.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ll.auto.JSONAuthentication;
import com.ll.entity.LType;
import com.ll.entity.LUser;
import com.ll.mapper.LTypeMapper;
import com.ll.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ll
 * @since 2022-03-15
 */
@RestController
@RequestMapping("/l-type")
public class LTypeController extends JSONAuthentication {
    @Autowired
    LTypeMapper lTypeMapper;
    @ApiOperation(value = "添加种类")
    @PostMapping("/add")
    public void addUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("name") String name) throws Exception {
        LType lType=new LType();
        lType.setName(name);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("name", name);
        if (lTypeMapper.selectOne(wrapper) != null) {
            Result error = Result.error().message("该种类已存在");
            this.WriteJSON(request, response, error);
        } else {
            if (lTypeMapper.insert(lType) > 0) {
                Result ok = Result.ok().message("注册成功");
                this.WriteJSON(request, response, ok);
            } else {
                Result error = Result.error().message("注册失败");
                this.WriteJSON(request, response, error);
            }
        }
    }
    @ApiOperation(value = "删除种类")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = lTypeMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找全部种类")
    @PostMapping("/findAllType")
    public void findAllType(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {

        Page<LType> page = new Page<>(current, size);
        Page<LType> pages = lTypeMapper.selectPage(page, null);
        List<LType> list = pages.getRecords();
        int total = lTypeMapper.selectCount(null);
        if (list.size() > 0) {
            Result ok = Result.ok().data("type", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有种类，快去添加一个吧");
        this.WriteJSON(request, response, error);
    }

}

